import request from "@/utils/request";

const AUTH_BASE_URL = "/api/v1/auth";
const GET_PHONE_CODE_URL = "/admin/login-captcha";
const LOGIN_URL = `/admin/login`;
const LOGINOUT_URL = `/admin/loginout`;

const AuthAPI = {
  /** 登录接口*/
  login(data: LoginFormData) {
    // const formData = new FormData();
    // formData.append("userName", data.username);
    // formData.append("password", data.password);
    // formData.append("captchaKey", data.captchaKey);
    // formData.append("captchaCode", data.captchaCode);
    // formData.append("phone", data.phone);
    // formData.append("smsCode", data.code);
    const formData = {
      userName: data.username,
      password: data.password,
      phone: data.phone,
      smsCode: data.code,
    };

    return request<any, LoginResult>({
      url: `${LOGIN_URL}`,
      method: "post",
      data: formData,
      // headers: {
      //   "Content-Type": "multipart/form-data",
      // },
    });
  },

  /** 刷新 token 接口*/
  refreshToken(refreshToken: string) {
    // return request<any, LoginResult>({
    //   url: `${AUTH_BASE_URL}/refresh-token`,
    //   method: "post",
    //   params: { refreshToken },
    //   headers: {
    //     Authorization: "no-auth",
    //   },
    // });
    return {
      code: "00000",
      data: {
        accessToken:
          "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsImRlcHRJZCI6MSwiZGF0YVNjb3BlIjoxLCJ1c2VySWQiOjIsImlhdCI6MTcyODE5MzA1MiwiYXV0aG9yaXRpZXMiOlsiUk9MRV9BRE1JTiJdLCJqdGkiOiJhZDg3NzlhZDZlYWY0OWY3OTE4M2ZmYmI5OWM4MjExMSJ9.58YHwL3sNNC22jyAmOZeSm-7MITzfHb_epBIz7LvWeA",
        tokenType: "Bearer",
        refreshToken:
          "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsImRlcHRJZCI6MSwiZGF0YVNjb3BlIjoxLCJ1c2VySWQiOjIsImlhdCI6MTcyODE5MzA1MiwiYXV0aG9yaXRpZXMiOlsiUk9MRV9BRE1JTiJdLCJqdGkiOiJhZDg3NzlhZDZlYWY0OWY3OTE4M2ZmYmI5OWM4MjExMSJ9.58YHwL3sNNC22jyAmOZeSm-7MITzfHb_epBIz7LvWeA",
        expires: null,
      },
      msg: "一切ok",
    };
  },

  /** 退出登录接口 */
  logout() {
    return request({
      url: `${LOGINOUT_URL}`,
      method: "post",
    });
  },

  /** 获取验证码接口*/
  getCaptcha() {
    return request<any, CaptchaInfo>({
      url: `${AUTH_BASE_URL}/captcha`,
      method: "get",
    });
  },

  /** 获取验证码接口*/
  getPhoneCode(data: PhoneCode) {
    return request<any>({
      url: `${GET_PHONE_CODE_URL}`,
      method: "post",
      data,
    });
  },
};

export default AuthAPI;

/** 登录表单数据 */
export interface LoginFormData {
  /** 用户名 */
  username: string;
  /** 密码 */
  password: string;
  /** 手机号 */
  phone: string;
  /** 手机号短信验证码 */
  code: string;
  /** 验证码缓存key */
  captchaKey: string;
  /** 验证码 */
  captchaCode: string;
  /** 记住我 */
  rememberMe: boolean;
}

/** 登录响应 */
export interface LoginResult {
  /** 访问令牌 */
  //accessToken: string;
  /** 刷新令牌 */
  //refreshToken: string;
  /** 令牌类型 */
  //tokenType: string;
  /** 过期时间(秒) */
  //expiresIn: number;

  /** 过期时间(秒) */
  expire: string;
  /** 过期时间(秒) */
  refresh_expire: string;
  /** 刷新令牌 */
  refresh_token: string;
  /** 访问令牌 */
  token: string;
}

/** 验证码信息 */
export interface CaptchaInfo {
  /** 验证码缓存key */
  captchaKey: string;
  /** 验证码图片Base64字符串 */
  captchaBase64: string;
}

/*手机号信息*/
export interface PhoneCode {
  /** 手机号 */
  phone: string;
}
